package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@CmdApi(reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.class, respMsg = CommonActivityProto.CommonActivityRankListRespMsg.class)
@Cmd(code = Protocol.S_GENERAL_ACTIVITY_USER_RANK, desc = "个人活动积分排行榜(仅排名)")
public class GeneralActivityUserRankCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;
    private List<Long> otherUserId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getRankType();
        otherUserId = reqMsg.getOtherUserIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        ActivityInfo openActivityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (null != openActivityInfo) {
            boolean isCrossRank = ActivityHelper.conditionTypeIsCrossRank(conditionType);
            if(!isCrossRank){
                RankListModel rankListModel = ActivityRankMgr.getActivityRankListModel(activityId, conditionType);
                if (null != rankListModel) {
                    respMsg.setMyRank(rankListModel.unlockGetMyRank(player.getUserId()));
                }
            } else {
                CommonActivityProto.CommonActivityRankListReqMsg.Builder crossReq = CommonActivityProto.CommonActivityRankListReqMsg.newBuilder();
                crossReq.setActivityId(activityId);
                crossReq.setRankType(conditionType);
                crossReq.addAllOtherUserId(otherUserId);
                int groupId = 0;
                if (UnionActivityGroupHelper.isGroupActivityType(openActivityInfo.getType())) {
                    groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
                }
                player.sendUnionActivityGroupPacket(Protocol.C_CROSS_GENERAL_USER_MY_RANK, crossReq, activityId, groupId);
                return;
            }
        }
        player.sendPacket(Protocol.U_GENERAL_ACTIVITY_USER_RANK, respMsg);
    }
}
